# coding=utf-8
import traceback

import pymysql.cursors

from tests.modules.ucloud.servers import servers
from tests.modules.ucloud.ucloud_db import UcloudApp


def deal_all_db(_ip):
    uc = UcloudApp(ip=_ip)

    try:

        uc.do_remove(app_name='packing-alg', zone_alias='auaws')
        uc.do_remove(app_name='packing-alg', zone_alias='euaws')
        uc.do_remove(app_name='packing-alg', zone_alias='gzidc')
        uc.do_remove(app_name='packing-alg', zone_alias='usaws')

        uc.do_insert(app_name='packing-alg',
                     zone='uc.auaws.area',
                     zone_alias='auaws',
                     port=30480,
                     app_type='tomcat',
                     db_name=None,
                     mem_level=2,
                     db_type=None)

        uc.commit()

        uc.do_insert(app_name='packing-alg',
                     zone='uc.euaws.area',
                     zone_alias='euaws',
                     port=20480,
                     app_type='tomcat',
                     db_name=None,
                     mem_level=2,
                     db_type=None)

        uc.commit()

        uc.do_insert(app_name='packing-alg',
                     zone='uc.gzidc.area',
                     zone_alias='gzidc',
                     port=6480,
                     app_type='tomcat',
                     db_name=None,
                     mem_level=2,
                     db_type=None)

        uc.commit()

        uc.do_insert(app_name='packing-alg',
                     zone='uc.usaws.area',
                     zone_alias='usaws',
                     port=10480,
                     app_type='tomcat',
                     db_name=None,
                     mem_level=2,
                     db_type=None)

        uc.commit()


    except BaseException, ex:
        print ex.message

    finally:

        uc.close()


servers = [
    "172.16.3.131"
]

for _ip in servers:
    try:
        print '准备刷新ucloud%s:' % _ip

        deal_all_db(_ip)

        print 'ucloud%s刷新完成:' % _ip

    except BaseException, ex:
        print ex.message
